package com.LastingRunner;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class StopWatchActivity extends Activity implements OnClickListener {

	private LinkedList<String> lstShowLapTime;
	private LinkedList<Number> lstLapTime;
	private ListView lapListView;
	private Chronometer chronoSplit;
	private Chronometer chronoTotal;
	private ToggleButton btnStartStop;
	private Button btnLap;
	private Button btnReset;
	private boolean started;
	private DecimalFormat SimpleTimeFormat = new DecimalFormat();
	
	private long savedChronoSplitBase;
	private long savedChronoTotalBase;
	private CharSequence savedChronoSplitText;
	private CharSequence savedChronoTotalText;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.stop_watch);
	    
	    lapListView = (ListView) findViewById(R.id.lapView);
	    lstShowLapTime = new LinkedList<String>();
	    lstLapTime = new LinkedList<Number>();
	    chronoSplit = (Chronometer) findViewById(R.id.split_chrono);
		chronoSplit.setText("00:00");
		chronoTotal = (Chronometer) findViewById(R.id.total_chrono);
		chronoTotal.setText("00:00");
	    
	    btnStartStop = (ToggleButton) findViewById(R.id.startstopButton);
	    btnLap = (Button) findViewById(R.id.lapButton);
	    btnReset = (Button) findViewById(R.id.resetButton);
	    
	    btnStartStop.setOnClickListener(this);
	    btnStartStop.setChecked(false);
	    btnLap.setOnClickListener(this);
	    btnReset.setOnClickListener(this);
	    
	    SimpleTimeFormat.setMinimumIntegerDigits(2);
	    started = false;
	    
	    updateLapListView();
	}
	
	@Override
	public void onSaveInstanceState(Bundle outstate){
		super.onSaveInstanceState(outstate);
		outstate.putBoolean("started", started);
		if (started){
			outstate.putSerializable("lstLapTime", lstLapTime);
			outstate.putSerializable("lstShowLapTime", lstShowLapTime);
			outstate.putLong("SplitBase", chronoSplit.getBase());
			outstate.putLong("TotalBase", chronoTotal.getBase());
		}
	}
	
	
	@Override
	public void onRestoreInstanceState(Bundle outstate){
		super.onRestoreInstanceState(outstate);
		started = outstate.getBoolean("started");
		if (started){
			lstLapTime = (LinkedList<Number>) outstate.getSerializable("lstLapTime");
			lstShowLapTime = (LinkedList<String>) outstate.getSerializable("lstShowLapTime");
			chronoSplit.setBase(outstate.getLong("SplitBase"));
			chronoTotal.setBase(outstate.getLong("TotalBase"));
			chronoTotal.start();
			chronoSplit.start();
			updateLapListView();
		}
	}
	
	private void updateLapListView(){
	    lapListView.setAdapter(new ArrayAdapter<String>(this,R.layout.date_list_item,lstShowLapTime));
		
	}



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btnStartStop){
			if (!started){
				chronoSplit.setBase(SystemClock.elapsedRealtime());
				chronoTotal.setBase(SystemClock.elapsedRealtime());
				chronoSplit.start();
				chronoTotal.start();
				started = true;
				
			} else {
				chronoSplit.stop();
				chronoTotal.stop();
				chronoSplit.setText("00:00");
				chronoTotal.setText("00:00");
				started = false;
			}
			
		} else if (v == btnLap){
			if (started){
				long total_sec = ((SystemClock.elapsedRealtime()-chronoSplit.getBase())/1000);
				lstShowLapTime.add(SimpleTimeFormat.format(total_sec/60) + ":" + SimpleTimeFormat.format(total_sec % 60));
				lstLapTime.add(total_sec);
				updateLapListView();
				chronoSplit.stop();
				chronoSplit.setBase(SystemClock.elapsedRealtime());
				chronoSplit.setText("00:00");
				chronoSplit.start();
			}
			
		} else if (v == btnReset){
			if (started){
				chronoSplit.stop();
				chronoSplit.setText("00:00");
				chronoTotal.stop();
				chronoTotal.setText("00:00");
				started = false;
				btnStartStop.toggle();
			}
			lstLapTime = new LinkedList<Number>();
			lstShowLapTime = new LinkedList<String>();
			updateLapListView();
			
		}
		
	}

}
